package com.health.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.common.PageBean;
import com.health.dto.CheckgroupQueryDTO;
import com.health.exception.CheckgroupCodeExistedException;
import com.health.exception.CheckgroupNameExistedException;
import com.health.exception.ParameterInvalidException;
import com.health.mapper.CheckgroupCheckitemMapper;
import com.health.mapper.CheckgroupMapper;
import com.health.pojo.Checkgroup;
import com.health.pojo.CheckgroupCheckitem;
import com.health.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 检查组业务实现
 *
 * @author zgp
 * @version V1.0
 * @date 2025-01-18 18:55
 */
@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;


    @Override
    public PageBean<Checkgroup> findPageList(CheckgroupQueryDTO checkgroupQueryDTO) {
        PageHelper.startPage(checkgroupQueryDTO.getCurrentPage(), checkgroupQueryDTO.getPageSize());
        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPageList(checkgroupQueryDTO);
        return new PageBean<>(checkgroupPage.getTotal(), checkgroupPage.getResult());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteCheckgroupById(Integer id) {
        if (Objects.isNull(id)) {
            throw new ParameterInvalidException("ID不能为空");
        }
        // 删除关联表中检查组对应的检查项
        checkgroupCheckitemMapper.deleteByCgId(id);
        // 删除检查组
        checkgroupMapper.deleteById(id);
    }

    @Override
    public Checkgroup findCheckgroupById(Integer id) {
        if (Objects.isNull(id)) {
            throw new ParameterInvalidException("ID不能为空");
        }
        return checkgroupMapper.selectById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        if (Objects.isNull(id)) {
            throw new ParameterInvalidException("ID不能为空");
        }
        return checkgroupCheckitemMapper.selectByCheckGroupId(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void addCheckgroupAndItems(Checkgroup checkgroup, List<Integer> checkitemIds) {
        // 参数校验
        if (CollUtil.isEmpty(checkitemIds)) {
            throw new ParameterInvalidException("检查组中检查项不能为空");
        }
        if (StrUtil.isEmpty(checkgroup.getCode())
                || StrUtil.isEmpty(checkgroup.getName())) {
            throw new ParameterInvalidException("检查组编码或名称不能为空");
        }
        // 编码重复校验
        Checkgroup existItem = checkgroupMapper.selectByCode(checkgroup.getCode());
        if (Objects.nonNull(existItem)) {
            throw new CheckgroupCodeExistedException("检查组编码已存在");
        }
        existItem = checkgroupMapper.selectByName(checkgroup.getName());
        // 名称重复校验
        if (Objects.nonNull(existItem)) {
            throw new CheckgroupNameExistedException("检查组名称已存在");
        }

        // 填充其它属性
        if (StrUtil.isEmpty(checkgroup.getSex())) {
            checkgroup.setSex("0");
        }

        // 新增检查组基本信息
        checkgroupMapper.insert(checkgroup);
        // 新增检查组对用检查项
        List<CheckgroupCheckitem> checkgroupCheckitems = new ArrayList<>();
        checkitemIds.forEach(o -> checkgroupCheckitems.add(new CheckgroupCheckitem(checkgroup.getId(), o)));
        checkgroupCheckitemMapper.insertOfBatch(checkgroupCheckitems);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateCheckgroupAndItems(Checkgroup checkgroup, List<Integer> checkitemIds) {
        // 参数校验
        if (CollUtil.isEmpty(checkitemIds)) {
            throw new ParameterInvalidException("检查组中检查项不能为空");
        }
        if (StrUtil.isEmpty(checkgroup.getCode())
                || StrUtil.isEmpty(checkgroup.getName())
                || Objects.isNull(checkgroup.getId())) {
            throw new ParameterInvalidException("检查组ID或编码或名称不能为空");
        }
        //  校验检查组编码及名称是否重复
        Checkgroup item = checkgroupMapper.selectById(checkgroup.getId());
        if (Objects.isNull(item)) {
            throw new ParameterInvalidException("修改的检查组不存在");
        }
        Checkgroup existCheckgroup = checkgroupMapper.selectByCode(checkgroup.getCode());
        if (Objects.nonNull(existCheckgroup) && !existCheckgroup.getId().equals(checkgroup.getId())) {
            throw new CheckgroupCodeExistedException("检查组编码已存在");
        }
        existCheckgroup = checkgroupMapper.selectByName(checkgroup.getName());
        if (Objects.nonNull(existCheckgroup) && !existCheckgroup.getId().equals(checkgroup.getId())) {
            throw new CheckgroupNameExistedException("检查组名称已存在");
        }
        // 填充其它属性
        if (StrUtil.isEmpty(checkgroup.getSex())) {
            checkgroup.setSex("0");
        }

        // 修改检查组基本信息
        checkgroupMapper.updateById(checkgroup);
        // 删除原检查组中的检查项
        checkgroupCheckitemMapper.deleteByCgId(checkgroup.getId());
        // 批量新增检查组中的检查项信息
        List<CheckgroupCheckitem> checkgroupCheckitems = new ArrayList<>();
        checkitemIds.forEach(o -> checkgroupCheckitems.add(new CheckgroupCheckitem(checkgroup.getId(), o)));
        checkgroupCheckitemMapper.insertOfBatch(checkgroupCheckitems);
    }

    @Override
    public List<Checkgroup> getAllCheckgroups() {
        return checkgroupMapper.selectAll();
    }
}
